package com.isczy.eduservice.controller.front;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.eduservice.entity.EduCourse;
import com.isczy.eduservice.entity.EduTeacher;
import com.isczy.eduservice.service.EduCourseService;
import com.isczy.eduservice.service.EduTeacherService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/teacherfront")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    /***
     * ***************************************
     * 功能描述:前台分页查询讲师的方法
     * @param page
     * @param limit
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:03】
     * @author modify:
     */
    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public DataResult getTeacherFrontList(@PathVariable long page, @PathVariable long limit) {
        try{
            Page<EduTeacher> pageTeacher = new Page<>(page,limit);
            Map<String,Object> dataMap = teacherService.getTeacherFrontList(pageTeacher);
            //返回分页所有数据
            return DataResult.builder()
                    .data(dataMap)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("前台分页查询讲师失败")
                    .params(ArrayUtil.toString(new Long[]{page,limit}))
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:前台讲师详情的功能
     * @param teacherId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:02】
     * @author modify:
     */
    @GetMapping("getTeacherFrontInfo/{teacherId}")
    public DataResult getTeacherFrontInfo(@PathVariable String teacherId) {
        try{
            //1 根据讲师id查询讲师基本信息
            EduTeacher eduTeacher = teacherService.getById(teacherId);
            //2 根据讲师id查询所讲课程
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id",teacherId);
            List<EduCourse> courseList = courseService.list(wrapper);
            return DataResult.builder()
                    .data("teacher",eduTeacher)
                    .data("courseList",courseList)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("前台讲师详情查询失败")
                    .params(teacherId)
                    .build();
        }

    }
}












